#ifndef __AIP_VTOZ_H__
#define __AIP_VTOZ_H__

#include "speech.h"
#include "voice.h"

namespace Vtz{
      class VoiceToChinese{
      public:
        // 务必替换百度云控制台中新建百度语音应用的 Api Key 和 Secret Key
        aip::Speech * client = new aip::Speech("117501432", "v41cowoGkDCERwAzKaa1SQL0", "I5TFIsfPS23ULl5AxjLr6sDGXdEl0yn6");  
        aip::Speech * client_text_to_chinese_voice = new aip::Speech("92189695", "rOuo1NfRVBHwpvyTyr50ggtI", "0noc0cd7NgowirG30kAWOpU45EAUTW4t");
        std::string talk_speed = "5";
        std::string talk_person = "4";
        std::string mp3_file_name = "ttv.mp3";
        std::string mp3_file_name_path = "./ttv.mp3";
        std::string pcm_file_name_path = "./urvoice.pcm";
        std::string pcm_file_name = "urvoice.pcm";
        VoiceToChinese(){

        }

        VoiceToChinese(int talkSpeed, int talkPerson, std::string mp3FileNamePath, std::string pcmFileNamePath){
            char param[50];
            memset(param, 0, sizeof(param));
            sprintf(param, "%d", talkSpeed);
            talk_speed.assign((const char* )param);

            memset(param, 0, sizeof(param));
            sprintf(param, "%d", talkPerson);
            talk_person.assign((const char* )param);     

            std::string result1 = mp3FileNamePath.substr(2);
            mp3_file_name = result1;
            mp3_file_name_path = mp3FileNamePath;
            pcm_file_name_path = pcmFileNamePath;

            std::string result2 = pcmFileNamePath.substr(2);
            pcm_file_name = result2;
            
        }

        // 文字转语音
        void get_gpt_text_to_chinese_voice(const std::string &text){
            std::ofstream ofile;
            std::string file_ret;
            std::map<std::string, std::string> options;
            options["spd"] = talk_speed;
            options["per"] = talk_person;
            ofile.open(mp3_file_name_path, std::ios::out | std::ios::binary);
            Json::Value result = client_text_to_chinese_voice->text2audio(text, options, file_ret);
            // 如果file_ret为不为空则说明合成成功，返回mp3文件内容
            if (!file_ret.empty())
            {
                // 合成成功保存文件
                ofile << file_ret;
                //std::cout << "语音合成成功，打开目录下的tts.mp3文件听听看" << std::endl;
                make_mp3_to_voice(mp3_file_name);
            } else {
                // 合成出错，打印错误信息
                std::cout << result.toStyledString();
            }            
        }

        // 语言转文字
        std::string get_user_voice_to_chinese(int duration_seconds){        
            std::cout << "请开始讲话-" << std::endl;
            // 打开麦克风获取语音并设置录制时间
            aip::Voice voice;
            voice.get_voice(duration_seconds, pcm_file_name);

            // 打印详细请求结果，可以打开查看详细请求内容
            client->setDebug(false);
            
            // 语音识别调用
            return ASR(client);
    
        }

        ~VoiceToChinese() {
            delete client;  // 释放内存
            delete client_text_to_chinese_voice;
        }

      private:

        std::string ASR(aip::Speech* client) {
            std::map<std::string, std::string> options;
            std::string file_content;
            aip::get_file_content(pcm_file_name_path.c_str(), &file_content);
            Json::Value result = client->recognize(file_content, "pcm", 16000, options);
            //std::cout << "语音识别本地文件结果:" << std::endl << result.toStyledString();

            if (result["err_no"].asInt() == 0) { // 检查是否成功
                std::string recognition_result = result["result"][0].asString(); // 提取识别结果

                // 如果需要将结果存储到变量中
                std::string final_result = recognition_result;
                return final_result;
                // 你可以在这里使用 final_result 做进一步处理
            }
            return "";
        }      

        void make_mp3_to_voice(std::string mp3_file_name){
          int ret = -1;
          char sys_cmd[50];
          memset(sys_cmd, 0, sizeof(sys_cmd));
          sprintf(sys_cmd, "%s %s", "mpg123 --quiet", mp3_file_name.c_str());
          ret = system(sys_cmd);
          if(ret != 0){
            std::cerr << "音频播放失败，返回码: " << ret << std::endl;  
          }
        }

      };
}

#endif